﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JQueryControlExtenders.BaseExtender;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace JQueryControlExtender.Contrib.WeekCalendar
{
    [TargetControlType(typeof(Panel))]
    public class WeekCalendarControl : ExtenderControl
    {
        public IEnumerable<WeekCalendarEventItem> CalendarEvents { get; set; }

        /// <summary>
        /// [ function(calendar)] – A function that can return a height in pixels for the calendar. If the height of the calendar is less than the space it takes to render it, the calendar will scroll within the timeslot region while the day column headers will remain fixed.
        /// </summary>
        public int Height { get; set; }

        /// <summary>
        /// [boolean | default: true] – Whether to display the navigation buttons of ‘today’, ‘last week’, ‘next week’.
        /// </summary>
        public bool ShowButtons { get; set; }

        /// <summary>
        /// [integer  | default: 0] – Determines what day of the week to start on. 0 = sunday, 1 = tuesday etc.
        /// </summary>
        public DayOfWeek FirstDayOfWeek { get; set; }

        /// <summary>
        /// [string | default: "M d, Y" ] – A format to use for dates displayed in the calendar.
        /// </summary>
        public string DateFormat { get; set; }

        /// <summary>
        /// [integer | default: 4] – The number of timeslots that will be available within an hour. Calendar events will stick to this grid if their corresponding start and end times are based on the grid being used.
        /// </summary>
        public int TimeslotsPerHour { get; set; }

        /// <summary>
        /// [string  | default: " to "] – The delimiter /separator to use between start and end times display in a calendar event.
        /// </summary>
        public string TimeSeparator { get; set; }

        /// <summary>
        /// [string | default: "h:i a" ] – A format to use for times displayed by the calendar.
        /// </summary>
        public string TimeFormat { get; set; }

        /** Client Side Events **/

        /// <summary>
        /// [function(calEvent, element)] – Called on creation of a new calendar event.
        /// </summary>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public string ClientEventNew { get; set; }

        /// <summary>
        /// [function(calEvent, element)] – Called on click of a calendar event.
        /// </summary>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public string ClientEventClick { get; set; }

        /// <summary>
        /// [function(calEvent, element)] – Called prior to rendering an event. Allows modification of the eventElement or the ability to return a different element.
        /// </summary>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public string ClientEventRender { get; set; }

        public WeekCalendarControl()
        {
        }

        protected override IEnumerable<ScriptDescriptor> GetScriptDescriptors(Control targetControl)
        {
            var cal = new JQueryScriptControlDescriptor("weekCalendar", targetControl.ClientID);

            //data: {events: this.get_dataSource()} }
            if(CalendarEvents != null && CalendarEvents.Count() > 0)
                cal.AddProperty("data", CalendarEvents);

            if (Height > 0)
                cal.AddEvent("height", string.Format("function($calendar){{ return {0}; }}", Height));
            if (!string.IsNullOrEmpty(DateFormat))
                cal.AddProperty("dateFormat", DateFormat);
            cal.AddProperty("buttons", ShowButtons);
            cal.AddProperty("firstDayOfWeek", (int)FirstDayOfWeek);
            if(TimeslotsPerHour > 0)
                cal.AddProperty("timeslotsPerHour", TimeslotsPerHour);
            if(!string.IsNullOrEmpty(TimeSeparator))
                cal.AddProperty("timeSeparator", TimeSeparator);
            if(!string.IsNullOrEmpty(TimeFormat))
                cal.AddProperty("timeFormat", TimeFormat);

            //send events
            if (!string.IsNullOrEmpty(ClientEventNew))
                cal.AddEvent("eventNew", ClientEventNew);

            if (!string.IsNullOrEmpty(ClientEventClick))
                cal.AddEvent("eventClick", ClientEventClick);

            if (!string.IsNullOrEmpty(ClientEventRender))
                cal.AddEvent("eventRender", ClientEventRender);

            yield return cal;
        }

        protected override IEnumerable<ScriptReference> GetScriptReferences()
        {
            return null;
        }
    }
}
